package com.linxz.framework.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import io.reactivex.subjects.PublishSubject;

/**
 * @author Linxz
 * 创建日期：2019年02月20日 19:45
 * version：1.0.0
 * 描述：
 */
public abstract class BaseFragment extends Fragment {
    public Activity activity;
    private Toast toast = null;
    protected LayoutInflater inflater;
    public View contentView;
    private ViewGroup container;
    public final PublishSubject<ActivityLifeCycleEvent> lifecycleSubject = PublishSubject.create();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity =  getActivity();
        Bundle b = getArguments();
        onGetBundle(b);
    }

    public void onGetBundle(Bundle bundle) {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        this.container = container;
        lifecycleSubject.onNext(ActivityLifeCycleEvent.CREATE);
        if (setLayout() instanceof Integer){
            contentView=inflater.inflate((int)setLayout(),container,false);
        }else if (setLayout() instanceof View){
            contentView= (View) setLayout();
        }else{
            throw new ClassCastException("type if setlayout() must be int or View!");
        }
        initUI();
        setListener();
        initData();
        return contentView;
    }

    public void startActivityResult(int requestCode, Bundle bundle, Class c) {
        Intent intent = new Intent();
        intent.putExtra("Bundle", bundle);
        intent.setClass(activity, c);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        lifecycleSubject.onNext(ActivityLifeCycleEvent.PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        lifecycleSubject.onNext(ActivityLifeCycleEvent.STOP);
        super.onStop();
    }

    @Override
    public void onDestroy() {
        lifecycleSubject.onNext(ActivityLifeCycleEvent.DESTROY);
        super.onDestroy();
    }

    public abstract Object setLayout();

    public abstract void initUI();

    public abstract void initData();

    public void setListener(){}

    /**
     * 默认时间LENGTH_LONG
     */
    public void showToast(String msg) {
        // UIHelper.showToast(activity, toast, msg);
        Toast.makeText(activity,msg+"",Toast.LENGTH_SHORT).show();
    }

    /**
     * @param msg
     * @param length 显示时间
     */
    protected void showToast(String msg, int length) {
        //UIHelper.showToast(activity, toast, msg);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        contentView = null;
        container = null;
        inflater = null;
    }

    public void setContentView(int layoutResID) {
        setContentView(inflater.inflate(layoutResID, container, false));
    }

    public void setContentView(View view) {
        contentView = view;
    }

    public View getContentView() {
        return contentView;
    }

    public <T extends View> T findViewById(int id) {
        if (contentView != null) {
            return contentView.findViewById(id);
        }
        return null;
    }

    /**
     * http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed
     * */
//    @Override
//    public void onDetach() {
//        super.onDetach();
//        try {
//            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
//            childFragmentManager.setAccessible(true);
//            childFragmentManager.set(this, null);
//        } catch (NoSuchFieldException e) {
//            throw new RuntimeException(e);
//        } catch (IllegalAccessException e) {
//            throw new RuntimeException(e);
//        }
//    }


}